<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/5-1'}">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/5-1'}">角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row>
      <el-col :span="3" :offset="20">
        添加角色
        <el-button circle type="primary" icon="el-icon-circle-plus" @click="newadd">
        </el-button>
      </el-col>
      <el-col :span="20" :offset="2">
        <el-table :data="tableData" stripe border>
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column prop="actor" label="角色名"></el-table-column>
          <el-table-column prop="name" label="创建人"></el-table-column>
          <el-table-column prop="createAt" label="创建时间" :formatter="dateFormat"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="danger" @click="del(scope.row)">删除</el-button>
              <el-button type="warning" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="2" :offset="3" style="margin-top: 10px">
        <el-pagination
          :page-sizes="[10, 20, 30, 50]"
          :page-size="parseInt(pager.size)"
          layout="total, sizes, prev, pager, next, jumper"
          :total="parseInt(total)"
          @current-change="handleCurrentChange"
          :current-page="parseInt(pager.page)"
          @size-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import axios from 'axios'
  import {
    rolelist,delrole
  } from "@/api/role/role";

  export default {
    name: "RoleManagement",
    inject: ['reload'],
    data() {
      return {
        tableData: [],
        total:'',
        pager: {
          page: '',
          size: ''
        }
      }
    },
    methods: {
      handleCurrentChange(val) {
        console.log(val);
        localStorage.rolepage = val;
        this.pager.page = val;
        this.getRoleInfo();
      },
      handleSizeChange(val) {
        this.pager.size = val;
        localStorage.rolesize = val;
        this.getRoleInfo();
      },
      dateFormat: function (row, column, cellValue) {
        var date = cellValue;
        if (date == undefined) {
          return "";
        }
        return this.$moment(cellValue).format("YYYY-MM-DD HH:mm:ss");
      },
      newadd() {
        this.$router.push({name: 'AddNewRole'})
      },
      getRoleInfo() {
        let params = {};
        this.pager.size=localStorage.rolesize?localStorage.rolesize:10;
        this.pager.page=localStorage.rolepage?localStorage.rolepage:1;
        params.page = this.pager.page;
        params.size = this.pager.size;
        rolelist(params).then(this.handleGetDataSucc)
      },
      handleGetDataSucc(res) {
        if (res.data) {
          this.tableData = res.data.UserList;
          console.log(res);
          this.total=res.data.totle;
        }
      },
      del(row) {
        let id=row.id;
        console.log(row.id);
        this.$confirm('<div>确定删除角色信息吗?</div><div style="font-size: 12px">删除后可能导致账号不可用</div>',
          '提示', {
            confirmButtonText: '确定',
            dangerouslyUseHTMLString: true,
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            let _this=this;
          delrole(id).then(function (res) {
            console.log(res);
            _this.$message({
              type: 'success',
              message: '已删除角色信息',
            });
            _this.reload();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      edit(row) {
        this.$router.push({name: 'EditNewRole', params: {index: row.id}})
      }
    },
    mounted() {
      this.getRoleInfo();
    }
  }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";

  .wrapper {
    .w100;
  }
</style>
